package org.meichao.utils;

import com.github.pagehelper.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

public class CommonUtils {

    private static Logger logger = LoggerFactory.getLogger(CommonUtils.class);

    /**
     * 获取当前用户的key
     * @param request
     * @return
     */
    public static String getCurrentUserKey(HttpServletRequest request){
        //先获取请求头中的"token",若不存在则获取请求参数中"token"
        String token = request.getHeader("token");
        String userKey = "";
        if(StringUtil.isEmpty(token)){
            token = request.getParameter("token");
        }
        logger.info("token:{}", token);
        try {
            userKey = JWTUtils.parseJWT(token).getId();
        } catch (Exception e) {
            logger.error("token异常{}:", e);
        }
        return userKey;
    }
}
